package main

func longestPalindrome(s string) string {
	// 检查自身是否满足
	if isABA(s) {
		return s
	}
	length := len(s)
	//结果长度和字符串
	relLength := 0
	relString := ""
	//双指针
	sl, sr := 0, length-1
	// 当剩余长度小于已有结果长度，则结束查找
	for sl < length-1-relLength {
		//左右指针相遇，sl右移1位，sr归最右侧
		if sl > sr {
			sl++
			sr = length - 1
		}
		var temp string
		// 如果左右两字符相同，则进行截取判断
		if s[sr] == s[sl] {
			temp = s[sl : sr+1]
			lenTemp := len(temp)
			if isABA(temp) && lenTemp > relLength {
				relLength = lenTemp
				relString = temp
			}
		}
		sr--
	}
	if relLength > 0 {
		return relString
	}
	return s[:1]
}

func isABA(s string) bool {
	sl, sr := 0, len(s)-1
	for sl < sr {
		if s[sl] != s[sr] {
			return false
		}
		sl++
		sr--
	}
	return true
}
